package com.bank.basic.service.impl;

import com.bank.basic.service.ScheduledService;
import com.bank.basic.service.TransferService;
import com.bank.basic.util.*;
import com.bank.common.commonUtil.TimeUtil;
import com.bank.model.entity.Transfer;
import com.bank.model.enums.TransferStatusNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.TimeUnit;


/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/4/2 11:58
 */
@Service
public class ScheduledServiceImpl implements ScheduledService {
    @Autowired
    EntityFactory entityFactory;
    @Autowired
    VoFactory voFactory;
    @Autowired
    ClientFactory clientFactory;
    @Autowired
    ServiceFactory serviceFactory;
    @Autowired
    MapperFactory mapperFactory;


    @Override
    @Scheduled(cron = " * 1 * * * 0-7")
    public void transfer() {
//        two hour transfer
        Queue<Transfer> transfers = mapperFactory.getScheduledObject().getTwoHourTransfer();
        if(transfers.size()<1) return;
        Transfer transfer = transfers.element();
        Date date = transfer.getCreatedTime();
        if(TimeUtil.addHour(date,2).before(new Date())){
            transfer.setStatus(TransferStatusNum.FINISHED.getCode());
            serviceFactory.getTransferService().calculateTransfer(transfer);
            serviceFactory.getTransferService().updateTransfer(transfer);
        }
//        one day transfer
        Queue<Transfer> transfers1 = mapperFactory.getScheduledObject().getOneDayTransfer();
        if(transfers1.size()<1) return;
        Transfer transfer1 = transfers1.element();
        Date date1 = transfer1.getCreatedTime();
        if(TimeUtil.addHour(date1,24).before(new Date())){
            transfer1.setStatus(TransferStatusNum.FINISHED.getCode());
            serviceFactory.getTransferService().calculateTransfer(transfer1);
            serviceFactory.getTransferService().updateTransfer(transfer1);
        }
    }

    @Override
    @Scheduled(cron = "* * 0 * * 0-7")
    public void deposit() {
        serviceFactory.getDepositService().calculateInterestRate();
    }

    @Override
    public void loan() {

    }
}
